package com.ai.module.chat.util;

import info.monitorenter.cpdetector.io.*;
import lombok.extern.slf4j.Slf4j;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class FileEncodingDetection {
    public static final String BASE_URL="D:\\goodBook\\玄幻";
    public static void main(String[] args) throws Exception {
//        String filePath = "F:\\资源ook\\仙侠小说\\109《仙界修仙》作者：莫默.txt";
//
//        String fileCharsetName = getFileCharsetName(new File(filePath));
//        System.out.println("fileCharsetName = " + fileCharsetName);
//
        File file = new File(BASE_URL);
        File[] files = file.listFiles();
        List<File> fileList = new ArrayList<>();
        for (File f : files) {
             if (f.isFile()){
                 fileList.add(f);
             }
            Charset fileCharsetName = getFileCharsetName(f);

        }

    }

    public void bufferReaderTest(String txtFile) {
        List<String> fileContentList = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(txtFile))) {
            String line;
            while ((line = bufferedReader.readLine())  != null) {
                // 处理每一行数据 .....
                sb.append(line);
                String content = sb.toString();
                if (content.length() > 512 * 1024){
                    fileContentList.add(content);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 获得文件编码
     *
     * @param file
     * @return
     * @throws Exception
     */

    public static Charset getFileCharsetName(File file) throws Exception {
        CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
        detector.add(new ParsingDetector(false));
        detector.add(JChardetFacade.getInstance());
        detector.add(ASCIIDetector.getInstance());
        detector.add(UnicodeDetector.getInstance());
        Charset charset = null;
        try {
            charset = detector.detectCodepage(file.toURI().toURL());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
//        Charset charsetName =new  GBK();
//        if (charset != null) {
//            if (charset.name().equals("US-ASCII")) {
//                charsetName = CharsetUtil.ISO_8859_1;
//               // charsetName = "ISO_8859_1";
//            } else if (charset.name().startsWith("UTF")) {
//                charsetName = charset.name();// 例如:UTF-8,UTF-16BE.
//            }  else if (charset.name().equals("windows-1252")){
//                //charsetName =new  UTF_16LE();
//               // charsetName = "UTF-16LE";
//            }
//        }
        return charset;//返回最终的编码格式
    }

    private static class CharsetDetector {
        public Charset detectCharset(Path path) throws Exception {
            byte[] bytes = Files.readAllBytes(path);
            CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
            decoder.reset();
            decoder.decode(ByteBuffer.wrap(bytes));
            return decoder.charset();
        }
    }

}